package com.xidian.zhiancloud.controller.systemset;

import com.xidian.zhiancloud.controller.base.BaseController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.enums.UserResponseEnums;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.model.syset.WorkFlowUserList;
import com.xidian.zhiancloud.service.user.UserService;
import com.xidian.zhiancloud.service.user.WorkFlowStepService;
import com.xidian.zhiancloud.service.user.WorkFlowUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 流程步骤顺序
 */
@RestController
@RequestMapping("/workflowstep")
public class WorkFlowStepController extends BaseController {

    @Autowired
    WorkFlowStepService workFlowStepService;

    @Autowired
    WorkFlowUserService workFlowUserService;

    @Autowired
    UserService userService;

    /**
     * 流程步骤顺序表
     * @param bpmlId 流程ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getWorkFlowStepList")
    public ResultDTO getWorkFlowStepList
        (@RequestParam(value="bpmlId") String bpmlId){
        return ResultDTO.successOf(workFlowStepService.getWorkFlowStepList
             (this.getSessionUserCompanyId(),bpmlId),
             GeneralResponseEnums.SUCCESS);
    }

    /**
     * 流程设置信息添加
     * @param workflowstepId 步骤ID
     * @param bpmlId 流程ID
     * @param userId 用户ID
     * @param jobId 职位ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/AddWorkFlowUserList")
    public ResultDTO AddWorkFlowUserList
        (@RequestParam(value="workflowstepId",required=false ,
                    defaultValue="0") String workflowstepId,
        @RequestParam(value="bpmlId",required=false ,
                    defaultValue="0") String bpmlId,
        @RequestParam(value="userId",required=false ,
                    defaultValue="0") String userId,
        @RequestParam(value="jobId",required=false ,
                    defaultValue="0") String jobId ){
        Long companyId =this.getSessionUserCompanyId();
        int rt = workFlowUserService.
                AddWorkFlowUserList
                        (companyId,workflowstepId,bpmlId,userId,jobId);
        if(rt!=1){
            return ResultDTO.errorOf(UserResponseEnums
                    .WORK_F_USER_PARAMETERS_ERROR);
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 流程设置信息
     *  @param workflowstepId 步骤ID
     *  @param bpmlId 流程ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getWorkFlowUserList")
    public ResultDTO getWorkFlowUserList
        (@RequestParam(value="workflowstepId") String workflowstepId,
         @RequestParam(value="bpmlId") String bpmlId){
        return ResultDTO.successOf(workFlowUserService.getWorkFlowUserList
              (this.getSessionUserCompanyId(),workflowstepId,bpmlId),
                GeneralResponseEnums.SUCCESS);
    }

    /**
     * 编辑流程设置
     * @param workFlowUser 流程设置类
     * @return 返回ResultDTO对象
     */
    @RequestMapping("/updateWorkFlowUser")
    public ResultDTO updateWorkFlowUser(WorkFlowUserList workFlowUser){
        Map<String,Object> in = new HashMap<String, Object>();
        List<WorkFlowUserList> res = workFlowUserService.getByFlChIdAndFlTyAndUsType
            (this.getSessionUserCompanyId(), workFlowUser.getFlowType(), 
                workFlowUser.getFlowChildId(),workFlowUser.getUserType());
        if(res.size() != 0){
            in.put("companyId", this.getSessionUserCompanyId());
            in.put("flowChildId", workFlowUser.getFlowChildId());
            in.put("flowType", workFlowUser.getFlowType());
            in.put("userId", workFlowUser.getUserId());
            in.put("userType", workFlowUser.getUserType());
            in.put("jobId", workFlowUser.getJobId());
            workFlowUserService.updateWorkFlowUser(in);
        }else if(res.size() == 0){
            in.put("id", workFlowUser.getId());
            in.put("companyId", this.getSessionUserCompanyId());
            in.put("flowChildId", workFlowUser.getFlowChildId());
            in.put("flowType", workFlowUser.getFlowType());
            in.put("userId", workFlowUser.getUserId());
            in.put("userType", workFlowUser.getUserType());
            in.put("jobId", workFlowUser.getJobId());
            workFlowUserService.addWorkFlowUser(in);    
        }
        return ResultDTO.successOf(GeneralResponseEnums.SUCCESS);
    }

    /**
     * 流程列表
     * @param flowType 流程类别 1 : 隐患排查治理类 ; 2 : 事故审批类 ; 3 : 危化品领用类
     * @return 返回ResultDTO对象
     */
    @RequestMapping("/getByFlowTypeAndWorkFlowUser")
    public ResultDTO getByFlowTypeAndUser(@RequestParam("flowType") Integer flowType){
        Long companyId =this.getSessionUserCompanyId();
        List<WorkFlowUserList> woFlUser=workFlowUserService.getByFlowTypeAndUser
            (companyId,flowType);
        return ResultDTO.successOf(woFlUser,GeneralResponseEnums.SUCCESS);
    }

    /**
     *  编辑中选择人员的列表
     * @param userType 用户枚举 : 
     *        dangerViolat : 三违处理人; dangerVote : 隐患判定人; 
     *        dangerCheck : 隐患核查人; dangerReview : 验收人; dangerDuty : 重大隐患负责人; 
     *        chemicalApproval : 危化品领用审批人; accidentCheck : 事故审核人; 
     *        accidentExec : 事故调查处理人;
     *                 
     * @return 返回ResultDTO对象
     */
    @RequestMapping("/getUserPersoList")
    public ResultDTO getUserPersoList(@RequestParam("userType") String userType) {
        Long companyId =this.getSessionUserCompanyId();
        String[] str = userType.split(",");
        List<String> jobAnduser = new ArrayList<>();
        List<SysUser> userList = new ArrayList<>();
        Map<String,List<SysUser>> map = new HashMap<>();
        for (int i = 0; i < str.length; i++) {
            List<WorkFlowUserList> woFlUser = workFlowUserService.getUserPersoList
                (1L, str[i]);
            if (woFlUser.size() > 0) {
                for (int j = 0; j < woFlUser.size(); j++) {
                    jobAnduser.add(String.valueOf(woFlUser.get(j).getJobId()));
                    jobAnduser.add(String.valueOf(woFlUser.get(j).getUserId()));
                }
                userList = userService.getByUserIdAndRoleIds(jobAnduser);
                map.put(str[i], userList);
            }
        }
        return ResultDTO.successOf(map,GeneralResponseEnums.SUCCESS);
    }
    
    /**
     * 获取事故审核人列表
     * @return
     */
    @RequestMapping("/getAccidentAudit")
    public ResultDTO getAccidentAudit() {
        Long companyId =this.getSessionUserCompanyId();
        List<Long> userID=workFlowUserService.getAccidentAudit(companyId);
        return ResultDTO.successOf(userID,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获取事故调查人列表
     * @return
     */
    @RequestMapping("/getAccidentSurvey")
    public ResultDTO getAccidentSurvey() {
        Long companyId =this.getSessionUserCompanyId();
        List<Long> userID=workFlowUserService.getAccidentAudit(companyId);
        return ResultDTO.successOf(userID,GeneralResponseEnums.SUCCESS);
    }

    /**
     * 获取危化品领用审批人列表
     * @return
     */
    @RequestMapping("/getByChemicalReviewerAndJobId")
    public ResultDTO getByChemicalReviewerAndJobId() {
        Long companyId =this.getSessionUserCompanyId();
        List<Long> userID=workFlowUserService.getByChemicalReviewerAndJobId (companyId) ;
        return ResultDTO.successOf (userID,GeneralResponseEnums.SUCCESS);
    }
    

}
